package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.project.entity.Factory;
import com.project.mapper.FactoryMapper;
import com.project.service.FactoryService;
import freemarker.template.utility.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lovo
 * @since 2023-07-21
 */

@Service
public  class FactoryServiceImp extends ServiceImpl<FactoryMapper, Factory> implements FactoryService {

    @Override
    public IPage<Factory> cutByItem(Integer pageNo, String name, String scope) {

        QueryWrapper qw = new QueryWrapper<>();
        if (name != null && name.length() != 0){
            qw.like("f_name",name);
        }
        if (scope != null && scope.length() != 0){
            qw.like("f_scope",scope);
        }

        return this.baseMapper.selectPage(new Page<>(pageNo,3),qw);
    }

    @Override
    public void addFactory(Factory factory) {

      int  number = this.baseMapper.findByName(factory.getName());

      if (number == 0){
          this.baseMapper.insert(factory);
      }

    }

    @Override
    public Factory findById(Integer id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public void updateHeadAndPhone(Integer id, String head, String headPhone) {
      Factory factory = this.baseMapper.selectById(id);
      factory.setHead(head);
      factory.setHeadPhone(headPhone);
      this.baseMapper.updateById(factory);

    }


}